/*
 * @Author: xinqiao lxq1990.0716@qq.com
 * @Date: 2023-11-28 15:48:43
 * @LastEditors: xinqiao lxq1990.0716@qq.com
 * @LastEditTime: 2024-09-03 18:13:15
 * @FilePath: \h5-components-demo\src\router\index.ts
 * @Description: 路由配置
 */
import { createRouter, createWebHashHistory } from 'vue-router'

const HOME_PAGE = '/home/index'

// 自动识别路由：以views目录下的文件结构作为路由
const fileMap = import.meta.glob('@/views/**/**.vue')
const firstRoutes = Array.from(new Set(Object.keys(fileMap).map((item) => item.split('/')[3])))

const routes:any[] = []
firstRoutes.forEach((firstRoute) => {
  const children = Object.entries(fileMap).filter((child) => child[0].indexOf(`/${firstRoute}/`) > -1)
  routes.push({
    path: firstRoute,
    name: firstRoute,
    children: children.map((child) => {
      const secondRoute = child[0].split('/')[4].split('.')[0]
      return {
        path: secondRoute,
        name: `${firstRoute}-${secondRoute}`,
        component: child[1]
      }
    })
  })
})

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '',
      name: '',
      redirect: HOME_PAGE,
      children: [ ...routes ]
    }
  ]
})

export default router
